package cn.jszhao.eform.config;

import cn.hutool.core.util.StrUtil;
import cn.jszhao.common.utils.ServletUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @auth: 紫麒麟vip
 * @create: 2023/7/31 16:38
 * Description:
 */
@Component
public class ServerConfig {
    /**
     * 获取完整的请求路径，包括：域名，端口，上下文访问路径
     *
     * @return 服务地址
     */
    public String getUrl() {
        HttpServletRequest request = ServletUtils.getRequest();
        return getDomain(request);
    }

    public static String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String contextPath = request.getServletContext().getContextPath();
        String otConfigActive = System.getenv("OT_CONFIG_ACTIVE");
        String result = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
        if (StrUtil.isNotEmpty(otConfigActive)) {
            if ("outertest".equals(otConfigActive)) {
                result = result.replace(":8080", ":8082");
            } else if ("outerprod".equals(otConfigActive)) {

            }
        } else {

        }
        return result;
    }
}
